<template>
    <div>
       <table class="table">
        <tbody>
            <tr>
                <td>姓名</td>
                <td><input type="text" v-model="data.userName"></td>
            </tr>
             <tr>
                <td>证件类型</td>
                <td>
                    <select v-model="data.userLei">
                        <option value="0">请选择</option>
                        <option v-for="a in dat":value='a.userLei'>{{a.name}}</option>
                    </select>
                </td>
            </tr>
             <tr>
                <td>性别</td>
                <td>
                    <input type="radio" value="1" name="sex" v-model="data.userSex">男
                    <input type="radio" value="1" name="sex" v-model="data.userSex">女
                </td>
            </tr>
             <tr>
                <td>职业</td>
                <td>
                    <select v-model="data.hoteId">
                        <option value="0">请选择</option>
                        <option v-for="a in datas":value='a.hoteId'>{{a.hoteName}}</option>
                    </select>
                </td>
            </tr>
             <tr>
                <td>手机号码</td>
                <td><input type="text" v-model="data.userPhone"></td>
            </tr>
             <tr>
                <td>证件号码</td>
                <td><input type="text" v-model="data.userZHao"></td>
            </tr>
             <tr>
                <td>生日</td>
                <td><input type="date" v-model="data.userTime"></td>
            </tr>
             <tr>
                <td>详细地址</td>
                <td><input type="text" v-model="data.userAddress"></td>
            </tr>
             <tr>
                <td></td>
                <td><input type="button" value="添加" @click="postdata"></td>
            </tr>
        </tbody>
       </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref , onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

const dat = [{userLei:1,name:'身份证'},{userLei:2,name:'军官证'},{userLei:3,name:'护照'}]

onMounted(()=>{
getdata();
})

const datas = ref([{
    "hoteId": 1,
    "hoteName": "老师"
  },])

const getdata=()=>{
    axios.get("/api/Hote/Show").then(res=>{
    datas.value = res.data
    })
}


const data = ref({
  "userId": 0,
  "hoteId": 0,
  "userName": "",
  "userLei": 0,
  "userSex": 0,
  "userPhone": "",
  "userZHao": "",
  "userTime": "2025-06-26T01:15:23.110Z",
  "userAddress": ""
})

const postdata=()=>{

    if(data.value.userName==""){
    alert("名字不能为空")
    return;
    }
    if(data.value.userLei==0){
    alert("证件类型不能为空")
    return;
    }
    if(data.value.userSex==0){
    alert("性别不能为空")
    return;
    }
    if(data.value.hoteId==0){
    alert("职业不能为空")
    return;
    }
    if(data.value.userPhone==""){
    alert("手机号码不能为空")
    return;
    }
    var lef = /^13\d{9}$/;
    if(!lef.test(data.value.userPhone)){
    alert("手机号不对")
    return;
    }
    if(data.value.userZHao==""){
    alert("证件号码不能为空")
    return;
    }
    if(data.value.userTime==""){
    alert("生日不能为空")
    return;
    }
    if(data.value.userAddress==""){
    alert("地址不能为空")
    return;
    }
    axios.post("/api/User/AddAsync",data.value).then(res=>{
    if(res.data>0){
    alert("添加成功");
    router.push("/List");
    }
    else{
    alert("添加失败"); 
    }
    })
}

</script>

<style scoped>

</style>